package controller;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;

import model.Model;
import view.View;

public class Controller extends WindowAdapter implements ActionListener {

	private Model model;
	private View view;
	
	private JFileChooser chooser;
	private String[] monthArray = new String[] {"Januar", "Februar", "M\u00E4rz", "April", "Mai", "Juni", "Juli", "August", "September", "Oktober", "November", "Dezember"};

	public Controller(View view) {
		this.model = new Model(this);

		// Handle openfile
		chooser = new JFileChooser();

		this.view = view;
		

		model.addObserver(view);
		if (view.getParentContainer() instanceof JFrame) {
			((JFrame) view.getParentContainer()).addWindowListener(this);
		}
	}

	public Model getModel() {
		return model;
	}

	public String switchMonthTitle(int month) {
		return monthArray[month];
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		String command = e.getActionCommand();
		System.out.println(command);
		String[] cmd = command.split("#");

		if (command.startsWith("style")) {
			try {
				UIManager.setLookAndFeel(cmd[1]);
			} catch (Exception error) {
				System.err.println("Sorry, could not load that style");
			} finally {
				SwingUtilities.updateComponentTreeUI(view);
			}
		} else if (command.startsWith("switchto")) {
			view.switchTo(cmd[1]);

			JButton chView = view.getBtnChangeView();
			if (cmd[1].equals("monthview")) {
				chView.setActionCommand("switchto#yearview");
				chView.setText("Details");
			} else {
				chView.setActionCommand("switchto#monthview");
				chView.setText("Start");
			}
		} else if (command.startsWith("add")) {
			if (cmd[1].equals("transaction")) {
				view.getTransactionForm().processForm(model);
				view.getTransactionForm().reset(); // TODO make good reset
			}
		} else if (command.startsWith("openfile")) {
			model.store();
			
			int chooserStatus = chooser.showOpenDialog(view);
			
			if(chooserStatus == JFileChooser.APPROVE_OPTION) {
				System.out.println("handling file");
				File dataFile = chooser.getSelectedFile();
				
				model.openDataFile(dataFile);
			}
			else {
				System.out.println("Selection cancelled by user");
			}
		}
		else if(command.startsWith("save")) {
			if(cmd[1].equals("now")) {
				model.store();
			}
			else if(cmd[1].equals("as")) {
				int chooserStatus = chooser.showSaveDialog(view);
				
				if(chooserStatus == JFileChooser.APPROVE_OPTION) {
					System.out.println("handling file");
					File newDataFile = chooser.getSelectedFile();
					
					model.saveTo(newDataFile);
				}
				else {
					System.out.println("Selection cancelled by user");
				}
			}
		}

	}

	@Override
	public void windowClosing(WindowEvent e) {
		System.out.println("SERIALIZE NOW");
		model.store();
	}
	
	public String[] getMonthArray() {
		return monthArray;
	}
	
	public int getIndexOf(String month) {
		for (int i = 0; i < monthArray.length; i++) {
			String arrMnth = monthArray[i];
			
			if(month.equals(arrMnth)) return i;
		}
		return 0;
	}
}
